package cn.bouncyslime.algorithms.tree.binaryTree;

// 给定两棵二叉树判断两个二叉树是否相同（结构相同，并且各个节点的值也相同）
public class SameTree {
    public static void main(String[] args){
        BinaryTree bt = new BinaryTree();

        int[] array = {1,2,3,4,5,0,0,0,0,6,7};
        int[] array2 = {1,2,3,4,5,0,0,0,0,6};
        TreeNode root = bt.createTree(array);
        TreeNode root2 = bt.createTree(array2);

        SameTree st = new SameTree();
        if(st.isSameTree(root, root2)){
            System.out.println("两个二叉树相同");
        }else{
            System.out.println("两个二叉树不同");
        }
    }

    public boolean isSameTree(TreeNode node, TreeNode node2){
        if(node==null && node2==null){
            return true;
        }
        if(node!=null && node2!=null && node.getValue()==node2.getValue()){
            if(isSameTree(node.getLeft(), node2.getLeft()) && isSameTree(node.getRight(), node2.getRight())){
                return true;
            }
        }
        return false;
    }
}